/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.io.FileOutputStream;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.Header;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author Avdoshkin
 */
public class Main {

    static class Trip{
        String number;
        String timeArrive;
        String timeStart;
        String timeRemain;
        String station;
        String cost = "";
        
        public String toString(){
            return station + " " + timeRemain + " " + cost + " " + number + " " +
                    timeStart + " " + timeArrive;
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws URISyntaxException, IOException {
        HttpClient httpclient = new DefaultHttpClient();
        FileOutputStream fos = null;
        try {
            URIBuilder builder = new URIBuilder();
            builder.setScheme("http").setHost("m.rasp.yandex.ru").setPath("/search")
                    .setParameter("fromName", "Долгопрудная")
                    .setParameter("fromId", "")
                    .setParameter("toName", "Москва (Савёловский вокзал)")
                    .setParameter("toId", "")
                    .setParameter("when", "2013-04-8")
                    .setParameter("when_interval", "6")
                    .setParameter("search_type", "suburban");
            URI uri = builder.build();
            HttpGet httpget = new HttpGet(uri);
            httpget.setHeader("Accept-Language", "en-US");
            httpget.setHeader("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
            httpget.setHeader("Accept", "application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            StringBuilder builder1 = new StringBuilder();
            for (Header h : httpget.getAllHeaders()) {
                builder1.append(h.getName() + ":" + h.getValue() + " ");
            }
            System.out.println(httpget.getMethod() + " " + builder1.toString() + " " + httpget.getURI());

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
//            System.out.println(responseBody);
            System.out.println("----------------------------------------");
            fos = new FileOutputStream("reply.xhtml");
            fos.write(responseBody.getBytes("Cp1251"));
            if (fos != null) {
                fos.close();
            }

            Document doc = Jsoup.parse(responseBody);
            Elements list = doc.select("ul.b-search-result > li");
            Element elArray[] = new Element[list.size()];
            list.toArray(elArray);
            for (int i=0; i< elArray.length; i++) {
                Element el1 = elArray[i].select("div > a.b-icon").first();
                Element el2 = elArray[i].select("div").first();
                Element el3 = elArray[i].select("div > b").first();
                Element el4 = elArray[i].select("div > em").first();
                Element el5 = elArray[i].select("em > span.b-currency").first();
                Trip t = new Trip();
                t.number = el1.text();
                t.timeStart = el3.text().substring(0, 5);
                t.timeArrive = el3.text().substring(5);
                t.timeRemain = el2.ownText();
                t.station = el4.text();
                if(el5 != null){
                    t.cost = el5.text();
                }
                System.out.println(t.toString());
//                Elements els2 = elArray[i].select("em > span.b-currency");
//                if (els2 != null) {
//                    Element el2 = els2.first();
//                    if (el2 != null) {
//                        System.out.println(el2.text());
//                    } else {
//                        System.out.println(elArray[i]);
//                    }
//                } else {
//                    int a = 1;
//                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        } finally {
            httpclient.getConnectionManager().shutdown();
            if (fos != null) {
                fos.close();
            }
        }

    }
}